package com.gmsk.pocket.controller;

import com.gmsk.pocket.entity.DataWrapper;
import com.gmsk.pocket.entity.DocType;
import com.gmsk.pocket.service.DocTypeService;
import com.gmsk.pocket.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "doctype")
public class DocTypeController {

    @Autowired
    DocTypeService docTypeService;


    /**
     * 根据节点id获取该节点的子节点列表
     * @param id
     * @return
     */
    @RequestMapping(value = "/list")
    public DataWrapper<List<DocType>> getResTypes(Integer id){
        List<DocType> resTypeList  = null;
        try {
            if (id == null) {
                resTypeList = docTypeService.findRootList();
                return new DataWrapper<>(DataWrapper.OK, "", resTypeList);
            }else {
                resTypeList = docTypeService.selectByParent(id);
                return new DataWrapper<>(DataWrapper.OK, "", resTypeList);

            }
        }catch (Exception e) {
            e.printStackTrace();
            return new DataWrapper<>(DataWrapper.ERROR, "error", resTypeList);
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public DataWrapper<DocType> add(Integer parentId, String name, String desc) {
        if (parentId == null) {
            return new DataWrapper<>(DataWrapper.ERROR, "parentId could not be null", null);
        }
        Integer max = 0;
        DocType parent = null;
        DocType docType = new DocType();
        docType.setName(name);
        docType.setDesc(desc);
        docType.setCreateTime(new Date());
        docType.setUpdateTime(new Date());
        docType.setHasChild(0);
        docType.setParentId(parentId);
        if(parentId != 0){
            parent = docTypeService.select(parentId);
            if (parent.getHasChild() == 1) {
                max = docTypeService.selectMaxSeq(parentId);
            }
        }else {
            max = docTypeService.selectMaxSeq(0);
        }
        docType.setSeq(max + 1);
        try {
            docTypeService.insert(docType);
            if(parentId != 0) { // 如果父节点id!=0, 则把父节点的hasChild置为1
                parent.setHasChild(1);
                docTypeService.update(parent);
            }
            return new DataWrapper<>(DataWrapper.OK, "", docType);
        }catch (Exception e) {
            e.printStackTrace();
            return new DataWrapper<>(DataWrapper.ERROR, "error", null);
        }
    }

    /**
     * 删除节点
     * @param id
     * @return
     *
     * 判断该节点有没有父节点，如果有，判断删除后获取父节点的子节点长度，如果为0，则将父节点的hasChild设置为0
     * */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    public DataWrapper<String> del(Integer id) {
        if (id == null) {
            return new DataWrapper<>(DataWrapper.ERROR, "error, id不能为空", null);
        }
        try {
            DocType resType = docTypeService.select(id);
            docTypeService.delete(resType.getId());
            if(resType.getParentId() != 0) {
                DocType parent = docTypeService.select(resType.getParentId());
                int size = docTypeService.selectByParent(parent.getId()).size();
                if (size == 0) {
                    parent.setHasChild(0);
                    docTypeService.update(parent);
                }
            }
            return new DataWrapper<>(DataWrapper.OK, "删除成功", "");
        }catch (Exception e) {
            e.printStackTrace();
            return new DataWrapper<>(DataWrapper.ERROR, "删除失败", "");
        }
    }

    /**
     * 编辑节点
     * @param id
     * @param name
     * @param desc
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public DataWrapper<DocType> edit(Integer id, String name, String desc) {
        if (id == null) {
            return new DataWrapper<>(DataWrapper.ERROR, "error, id不能为空", null);
        }
        if (name == null) {
            return new DataWrapper<>(DataWrapper.ERROR, "error, name不能为空", null);
        }
        try{
            DocType resType = docTypeService.select(id);
            resType.setName(name);
            resType.setDesc(desc);
            resType.setUpdateTime(new Date());
            docTypeService.update(resType);
            return new DataWrapper<>(DataWrapper.OK, "编辑成功", resType);
        }catch (Exception e) {
            e.printStackTrace();
            return new DataWrapper<>(DataWrapper.ERROR, "编辑失败", null);
        }
    }
}
